import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'

// https://vite.dev/config/
export default defineConfig({
  esbuild: {
    drop: ['console', 'debugger']
  },
  build: {
    rollupOptions: {
      output: {
        // 静态资源分类打包
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        // 非入口 chunk 文件
        chunkFileNames: 'js/chunks/[name]-[hash].js',
        // 入口文件
        entryFileNames: 'js/[name]-[hash].js'
      }
    }
  },
  plugins: [
    vue(),
    // 自动导入Vue相关函数
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'],
      dts: 'src/types/auto-imports.d.ts',
      // 解决eslint报错问题
      eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json'
      }
    }),
    // 自动导入组件
    Components({
      // 指定组件位置，默认是src/components
      dirs: ['src/components', 'src/Layout'],
      extensions: ['vue'],
      dts: 'src/types/components.d.ts'
    })
  ],
  // 配置 @ 路径别名
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@components': resolve(__dirname, 'src/components'),
      '@images': resolve(__dirname, 'src/assets/images'),
      '@views': resolve(__dirname, 'src/views')
    }
  },
  // 配置代理
  server: {
    host: '0.0.0.0'
    // proxy: {
    //   '/api': {
    //     target: 'http://localhost:3000',
    //     changeOrigin: true,
    //     rewrite: (path) => path.replace(/^\/api/, '')
    //   }
    // }
  }
})
